package com.handpay.zztong.hp.db.ormlite;

import android.content.Context;

import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.notification.bean.PushMessgaeBody;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fplei on 2017/5/17.
 * 推送消息数据操作
 */

public class DataBasePushMessageModel {
    private static final String TAG="DataBasePushMessageModel";
    private DataBaseHelper mDataBaseHelper=null;
    private Dao<PushMessgaeBody, Integer> noticeDao=null;
    private static DataBasePushMessageModel instance=null;

    public DataBasePushMessageModel(Context mContext){
        mDataBaseHelper=DataBaseHelper.getDBHelper(mContext);
        noticeDao=getPushMessageDao();
    }

    public static DataBasePushMessageModel getInstance(Context mContext){
        if(instance==null){
            synchronized (DataBaseHelper.class){
                instance=new DataBasePushMessageModel(mContext);
            }
        }
        return instance;
    }

    private Dao<PushMessgaeBody, Integer> getPushMessageDao(){
        try {
            if (noticeDao == null) {
                noticeDao = mDataBaseHelper.getDao(PushMessgaeBody.class);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return noticeDao;
    }

    /**
     * 添加一条消息
     * @param messgaeBody
     */
    public void addMessage(PushMessgaeBody messgaeBody){
        try {
            noticeDao.create(messgaeBody);
        }catch (Exception e){
            HPLog.e(TAG, "Add PushMessgaeBody faile！" + e);
        }
    }

    /**
     * 删除
     * @param messgaeBody
     */
    public void deleteMessage(PushMessgaeBody messgaeBody){
        try {
            noticeDao.delete(messgaeBody);
        }catch (Exception e){
            HPLog.e(TAG, "deleteMessage PushMessgaeBody faile！" + e.toString());
        }
    }

    /**
     * 是否存在某条消息
     * @param messgaeBody
     * @return
     */
    public boolean isExistMessage(PushMessgaeBody messgaeBody){
        if(messgaeBody==null)return false;
        PushMessgaeBody tempBody=queryMessage(messgaeBody.getId());
        if(tempBody!=null){
            return true;
        }
        return true;
    }

    /**
     * 查询消息
     * @param id
     * @return
     */
    public PushMessgaeBody queryMessage(int id){
        PushMessgaeBody body=null;
        try {
            body = noticeDao.queryForId(id);
        }catch (Exception e){
            HPLog.e(TAG, "queryMessage PushMessgaeBody faile！" + e.toString());
        }
        return body;
    }

    /**
     * 查询所有
     * @param userId
     * @return
     */
    public List<PushMessgaeBody> queryMessages(String userId){
        List<PushMessgaeBody> results=new ArrayList<>();
        try {
            QueryBuilder<PushMessgaeBody,Integer> builder=noticeDao.queryBuilder().offset(0L).limit(20L);
            //根据最新时间倒叙20条记录
            results =builder.orderBy("id", false).where().eq("userId", userId).query();
        }catch (Exception e){
            HPLog.e(TAG, "queryMessages PushMessgaeBody faile！" + e.toString());
        }
        return results;
    }
}
